package com.yanqu.road.server.pb;

import com.yanqu.road.entity.activity.lanruosi.LanRuoSiConfig;
import com.yanqu.road.entity.activity.lanruosi.LanRuoSiUserData;
import com.yanqu.road.entity.activity.lanruosi.config.LanRuoSiBlockConfigInfo;
import com.yanqu.road.entity.activity.lanruosi.config.LanRuoSiNpcInfo;
import com.yanqu.road.entity.activity.lanruosi.config.LanRuoSiScoreConfigInfo;
import com.yanqu.road.entity.activity.tripletown.*;
import com.yanqu.road.pb.activity.LanRuoSiProto;
import com.yanqu.road.pb.activity.TripleTownProto;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.lanruosi.LanRuoSiMgr;

public class LanRuoSiPb {

    /**
     * 配置
     */
    public static LanRuoSiProto.LanRuoSiSyncConfigRespMsg.Builder parseSyncConfigRespMsg(LanRuoSiConfig config, String language) {
        LanRuoSiProto.LanRuoSiSyncConfigRespMsg.Builder builder = LanRuoSiProto.LanRuoSiSyncConfigRespMsg.newBuilder();
        for (LanRuoSiBlockConfigInfo blockConfig : config.getBlockConfigMap().values()) {
            builder.addBlockConfigList(parseBlockConfigMsg(blockConfig, language));
        }
        for (LanRuoSiScoreConfigInfo scoreConfig : config.getScoreConfigMap().values()) {
            builder.addScoreConfigList(parseScoreConfigMsg(scoreConfig));
        }
        for (LanRuoSiNpcInfo npcInfo : config.getMonsterInfoMap().values()) {
            builder.addMonsterConfig(parseMonsterInfoTemp(npcInfo, language));
        }
        return builder;
    }

    private static LanRuoSiProto.LanRuoSiMonsterConfigTemp.Builder parseMonsterInfoTemp(LanRuoSiNpcInfo npcInfo, String language) {
        LanRuoSiProto.LanRuoSiMonsterConfigTemp.Builder builder = LanRuoSiProto.LanRuoSiMonsterConfigTemp.newBuilder();
        builder.setId(npcInfo.getId());
        builder.setNpcId(npcInfo.getNpcId());
        builder.setNpcHp(npcInfo.getNpcHp());
        builder.setPicSize(npcInfo.getPicSize());
        builder.setNpcName(ServerLanguageMgr.getContent(npcInfo.getNpcName(), language));
        builder.setNpcDesc(ServerLanguageMgr.getContent(npcInfo.getNpcDesc(), language));
        return builder;
    }

    public static TripleTownProto.TripleTownBlockConfigMsg.Builder parseBlockConfigMsg(LanRuoSiBlockConfigInfo config, String language) {
        TripleTownProto.TripleTownBlockConfigMsg.Builder builder = TripleTownProto.TripleTownBlockConfigMsg.newBuilder();
        builder.setId(config.getId());
        builder.setName(ServerLanguageMgr.getContent(config.getName(), language));
        builder.setType(config.getType());
        return builder;
    }

    public static TripleTownProto.TripleTownScoreConfigMsg.Builder parseScoreConfigMsg(LanRuoSiScoreConfigInfo config) {
        TripleTownProto.TripleTownScoreConfigMsg.Builder builder = TripleTownProto.TripleTownScoreConfigMsg.newBuilder();
        builder.setId(config.getId());
        builder.setContinuousNum(config.getContinuousNum());
        builder.setPic(config.getPic());
        builder.setScoreAdd(config.getScoreAdd());
        builder.setHarmAdd(config.getHarmAdd());
        builder.setNpcScoreAdd(config.getNpcScoreAdd());
        return builder;
    }

    /**
     * 游戏数据
     */
    public static LanRuoSiProto.LanRuoSiGameDataMsg.Builder parseGameDataMsg(LanRuoSiUserData userData, int multi) {
        LanRuoSiProto.LanRuoSiGameDataMsg.Builder builder = LanRuoSiProto.LanRuoSiGameDataMsg.newBuilder();
        int[][] blockList = userData.getBlockList(multi);
        for (int y = 0; y < blockList.length; y++) {
            for (int x = 0; x < blockList[y].length; x++) {
                builder.addBlockMsgList(parseBlockMsg(x, y, blockList[y][x]));
            }
        }
        for (Monster monster : userData.getMonsterList()) {
            builder.addMonster(parseMonsterTemp(monster));
        }
        builder.setWaveNum(userData.getWaveNum());
        builder.setEnergy(userData.getEnergy());
        builder.setLastRecTime(userData.getLastRecTime());
        builder.setScore(userData.getScore());
        builder.setMulti(multi);
        builder.setAllWaveNum(LanRuoSiMgr.getConfig().getWaveMonsterMap().size());
        return builder;
    }

    /**
     * 方块（不带积分）
     */
    public static TripleTownProto.TripleTownBlockMsg.Builder parseBlockMsg(int x, int y, int c) {
        TripleTownProto.TripleTownBlockMsg.Builder builder = TripleTownProto.TripleTownBlockMsg.newBuilder();
        builder.setX(x);
        builder.setY(y);
        builder.setColor(c);
        builder.setScore(0);
        return builder;
    }

    /**
     * 方块
     */
    public static TripleTownProto.TripleTownBlockMsg.Builder parseBlockMsg(Block block) {
        TripleTownProto.TripleTownBlockMsg.Builder builder = TripleTownProto.TripleTownBlockMsg.newBuilder();
        builder.setX(block.getX());
        builder.setY(block.getY());
        builder.setColor(block.getC());
        builder.setScore(block.getScore());
        builder.setAttackDamage(block.getAttackDamage());
        builder.setAttackScore(block.getAttackScore());
        return builder;
    }

    /**
     * 每阶段奖励
     */
    public static TripleTownProto.TripleTownEliminateStageMsg.Builder parseEliminateStageMsg(EliminateStageResult stageResult) {
        TripleTownProto.TripleTownEliminateStageMsg.Builder builder = TripleTownProto.TripleTownEliminateStageMsg.newBuilder();
        for (Boom eliminateBoom : stageResult.getBoomList()) {
            builder.addBoomList(parseBoomMsg(eliminateBoom));
        }
        for (Block block : stageResult.getBlockList()) {
            builder.addBlockList(parseBlockMsg(block));
        }
        return builder;
    }

    /**
     * 爆炸信息
     */
    public static TripleTownProto.TripleTownBoomMsg.Builder parseBoomMsg(Boom boom) {
        TripleTownProto.TripleTownBoomMsg.Builder builder = TripleTownProto.TripleTownBoomMsg.newBuilder();
        builder.setX(boom.getX());
        builder.setY(boom.getY());
        builder.setType(boom.getType());
        return builder;
    }

    /**
     * 每轮数据
     */
    public static TripleTownProto.TripleTownEliminateRoundMsg.Builder parseEliminateRoundMsg(EliminateRoundResult result) {
        TripleTownProto.TripleTownEliminateRoundMsg.Builder builder = TripleTownProto.TripleTownEliminateRoundMsg.newBuilder();
        builder.setType(result.getType());
        for (EliminateStageResult stageResult : result.getStageResultList()) {
            builder.addStageList(parseEliminateStageMsg(stageResult));
        }
        for (Block block : result.getCreateBomb()) {
            builder.addCreateBombList(parseBlockMsg(block));
        }
        for (int y = 0; y < result.getBlockList().length; y++) {
            for (int x = 0; x < result.getBlockList()[y].length; x++) {
                builder.addBlockMsgList(parseBlockMsg(x, y, result.getBlockList()[y][x]));
            }
        }
        return builder;
    }

    /**
     * 妖怪
     */
    public static TripleTownProto.TripleTownMonsterTemp.Builder parseMonsterTemp(Monster monster){
        TripleTownProto.TripleTownMonsterTemp.Builder builder = TripleTownProto.TripleTownMonsterTemp.newBuilder();
        builder.setId(monster.getId());
        builder.setHp(monster.getHp());
        builder.setBlood(monster.getBlood());
        builder.addAllCol(monster.getCols());
        builder.setX(monster.getX());
        builder.setY(monster.getY());
        return builder;
    }
}
